#include "main.h"
#include "stm32l5xx_hal.h"
#include "MAX31343.h"
#include "MAX31343_PlatformSpecific.h"
#include <string.h>
#include <stdio.h>
#include "cmsis_gcc.h"

UART_HandleTypeDef uart;

void SystemClock_Config(void);
static int UART_Init(void);
static int UART_PrintString(char *string);
static int UART_PrintFormattedInteger(char *format, int number);

#define ASSERT(status) do { if (!(status)) { __BKPT(42); } } while (0)

int max31343InterruptFlag = 0;

int main(void) {
	MAX31343_Status mStatus;
	int uStatus;

	// platform specific init
	HAL_Init();
	SystemClock_Config();

	// UART init
	uStatus = UART_Init();
	ASSERT(uStatus == 0);

	// initialize MAX31343 library
	mStatus = MAX31343_Init();
	ASSERT(mStatus == MAX31343_Status_Ok);

	// enable interrupt reception on this MCU (we will enable interrupt generation by RTC chip later)
	mStatus = MAX31343_InitInterruptsSupport();
	ASSERT(mStatus == MAX31343_Status_Ok);

	uStatus = UART_PrintString("========= Example 90_Interrupts =========\r\n");
	ASSERT(uStatus == 0);

	// configure alarm 1 to trigger every minute at 42 seconds.
	// alarm triggers for example at: 12:54:42, then at 12:55:42, then at 12:56:42, ...
	mStatus = MAX31343_Alarm1_ConfigureEveryMinute(42, 0);
	ASSERT(mStatus == MAX31343_Status_Ok);

	// enable generating interrupt
	mStatus = MAX31343_EnableInterrupts(MAX31343_Interrupt_Alarm1);
	ASSERT(mStatus == MAX31343_Status_Ok);

	while (1) {
		if (max31343InterruptFlag) {
			max31343InterruptFlag = 0;

			MAX31343_Interrupt flags;

			mStatus = MAX31343_GetPendingInterrups(&flags);
			ASSERT(mStatus == MAX31343_Status_Ok);

			// check if alarm triggered interrupt
			if (flags & MAX31343_Interrupt_Alarm1) {
				// get date and time from chip in normal format
				MAX31343_DateTime dt;
				mStatus = MAX31343_GetDateTime(&dt, 0); // 0 = normal (non-BCD) format
				ASSERT(mStatus == MAX31343_Status_Ok);

				// print date and time
				uStatus |= UART_PrintString("Alarm triggered at: ");

				uStatus |= UART_PrintFormattedInteger("%d", 2000 + dt.year);
				uStatus |= UART_PrintString("-");
				uStatus |= UART_PrintFormattedInteger("%02d", dt.month);
				uStatus |= UART_PrintString("-");
				uStatus |= UART_PrintFormattedInteger("%02d", dt.date);
				uStatus |= UART_PrintString(" ");
				uStatus |= UART_PrintFormattedInteger("%d", dt.hours);
				uStatus |= UART_PrintString(":");
				uStatus |= UART_PrintFormattedInteger("%02d", dt.minutes);
				uStatus |= UART_PrintString(":");
				uStatus |= UART_PrintFormattedInteger("%02d", dt.seconds);
				uStatus |= UART_PrintString("\r\n");
				ASSERT(uStatus == 0);
			}
		}
		__WFI();
	}
}

void EXTI15_IRQHandler() {
	if (__HAL_GPIO_EXTI_GET_FLAG(MAX31343_INT_GPIO_PIN)) {
		HAL_GPIO_EXTI_IRQHandler(MAX31343_INT_GPIO_PIN);
		max31343InterruptFlag = 1;
	}
}

/**
 * Generated by STM32CubeIDE
 */
void SystemClock_Config(void) {
	RCC_OscInitTypeDef RCC_OscInitStruct = { 0 };
	RCC_ClkInitTypeDef RCC_ClkInitStruct = { 0 };

	/** Configure the main internal regulator output voltage
	 */
	if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE0) != HAL_OK) {
		Error_Handler();
	}
	/** Initializes the RCC Oscillators according to the specified parameters
	 * in the RCC_OscInitTypeDef structure.
	 */
	RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
	RCC_OscInitStruct.MSIState = RCC_MSI_ON;
	RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
	RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
	RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
	RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
	RCC_OscInitStruct.PLL.PLLM = 1;
	RCC_OscInitStruct.PLL.PLLN = 55;
	RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
	RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
	RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
	if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
		Error_Handler();
	}
	/** Initializes the CPU, AHB and APB buses clocks
	 */
	RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
	RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
	RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
	RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
	RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

	if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) {
		Error_Handler();
	}
}

static int UART_Init(void) {
	HAL_StatusTypeDef hStatus;

	__HAL_RCC_GPIOG_CLK_ENABLE();
	__HAL_RCC_LPUART1_CLK_ENABLE();
	HAL_PWREx_EnableVddIO2();

	GPIO_InitTypeDef gpio;
	gpio.Pin = LPUART1_TX_Pin | LPUART1_RX_Pin;
	gpio.Mode = GPIO_MODE_AF_PP;
	gpio.Pull = GPIO_NOPULL;
	gpio.Speed = GPIO_SPEED_FREQ_LOW;
	gpio.Alternate = GPIO_AF8_LPUART1;

	HAL_GPIO_Init(GPIOG, &gpio);

	uart.Instance = LPUART1;
	uart.Init.BaudRate = 115200;
	uart.Init.WordLength = UART_WORDLENGTH_8B;
	uart.Init.StopBits = UART_STOPBITS_1;
	uart.Init.Parity = UART_PARITY_NONE;
	uart.Init.Mode = UART_MODE_TX;
	uart.Init.HwFlowCtl = UART_HWCONTROL_NONE;
	uart.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
	uart.Init.ClockPrescaler = UART_PRESCALER_DIV1;
	uart.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
	uart.FifoMode = UART_FIFOMODE_DISABLE;

	hStatus = HAL_UART_Init(&uart);
	if (hStatus != HAL_OK) {
		return -1;
	}

	hStatus = HAL_UARTEx_DisableFifoMode(&uart);
	if (hStatus != HAL_OK) {
		return -1;
	}

	return 0;
}

static int UART_PrintString(char *string) {
	HAL_StatusTypeDef hStatus;

	size_t len = strlen(string);
	hStatus = HAL_UART_Transmit(&uart, (unsigned char*) string, len, 1000);
	if (hStatus) {
		return -1;
	}

	return 0;
}

static int UART_PrintFormattedInteger(char *format, int number) {
	char buffer[32];
	int retval;

	retval = snprintf(buffer, sizeof(buffer), format, number);
	if (retval < 0 || retval >= sizeof(buffer)) {
		return -1;
	}

	return UART_PrintString(buffer);
}

/**
 * Error handler used by some HAL code
 */
void Error_Handler(void) {
	__disable_irq();
	while (1) {
		__WFI();
	}
}
